// pages/phonelist/phonelist.js
const app = getApp();
const httpUtils = require('../../utils/httpUtils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://yxpmaterial.sse100.com/mall/',
    mData: {
      id: null,
      username: null,
      mobile: null,
      isChoose: 0
    },
    userid:null,
    prodect_id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.prodect_id = options.id
  },

  getDate: function () {
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.childlist({
      access_token: app.globalData.$TOKEN,
      type: 0
    }).then((res) => {
      this.mData = res.data.lists
      res.data.lists.forEach(function (item, index) {
        if (index == 0) {
          item.isChoose = 1
        } else {
          item.isChoose = 0
        }
      })
      this.setData({
        mData: res.data.lists,
        userid:this.mData[0].id
      })
    })
  },
  //切换手机号
  chooseItem: function (e) {
let that=this
    var id = e.currentTarget.dataset['id'];
    console.log("id:" + id)
    this.data.mData.forEach(function (item, index) {
      if (item.id == id) {
        if (item.isChoose == 0) {
          item.isChoose = 1
          that.data.userid=item.id
         
        }
      } else {
        item.isChoose = 0
      }
    })

    this.setData({
      mData: this.data.mData,
      userid:this.data.userid
    })
  },
  next: function () {
    //下一步
   this.checkorder()
  },
  checkorder:function(){
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.checkorder({
      access_token: app.globalData.$TOKEN,
      userid: this.data.userid,
      prodect_id: this.data.prodect_id,
      doubleminus_type: app.globalData.userDetail.doubleminus_type,
    }).then((res) => {
      wx.hideLoading()
      wx.redirectTo({
        url: '/pages/referOrder/referOrder?id='+this.data.prodect_id+"&userid="+this.data.userid,
      })
    })
  },
  
  addphone: function () {
    //添加手机号
    wx.navigateTo({
      url: '/pages/addphone/addphone',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDate()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.showSearchResult) {
      this.getData();
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})